import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;


public class ReplacementSelection {
	PriorityQueue pq;
	
	
	public ReplacementSelection(){
	}

	
	public void buildHeap(){
		
	}
	
	public void createRuns(File file, int heapsize){
		int[] heap = new int[heapsize];
		populateHeap();
		
		// While there are elements to be read
		while (!fileIsEmpty()){
			int last = heapsize -1;
			
			// turn the array into a minheap
			buildHeap();
			
			// while there are elements to be read 
			// and there are still elements in the heap
			while(last >= -1 && !fileIsEmpty()){
				int next = getNext();
				
				//write root node to the output file 
				writeToFile(heap[0]);
				
				// if the next element is bigger then the root
				// put it as root
				if(next >= heap[0]){
					heap[0] = next;
				} else {
					// if the next element is smaller then the root
					// put the last element into the root					
					heap[0] = heap[last];
					
					// put the next element into the old slot of the last element
					heap[last] = next;
					
					// mark the last element as deadspace
					last --;
				}
				// rearrange the heap into a minheap
				siftDown();
			}
		}
	}

	public int getNext(){
		// TO BE ADDED
		return 5;
	}

	public void writeToFile(int element){
		// write to external file
	}
	
	public void siftDown(){
		
	}
	
	public void populateHeap(){
		
	}
	
	public int[] readElementsFromFile(int amountOfElements){
		int[] heap = new int[amountOfElements];
		try {
			BufferedReader bw = new BufferedReader(new FileReader(new File("input.txt")));
			for (int i=0; i<amountOfElements; i++){
				heap[i] = Integer.parseInt(bw.readLine());
			}
			bw.close();		
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return heap;
	}
	
	public boolean fileIsEmpty(){
		// TO BE ADDED
		return false;
	}
	
	

	
	
	
}
